package org.six11.ss.client;

import java.util.List;

import org.jdom.Element;
import org.six11.ss.shared.Message;
import org.six11.util.adt.MultiState;

/**
 * 
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public class ListRoomMessageHandler extends AWTMessageHandler {

  /**
   * 
   */
  public ListRoomMessageHandler(MultiState state) {
    super(state);
  }

  @SuppressWarnings("unchecked")
  @Override
  public void handleAWT(Message msg) {
    bug("Yay. I got a list of rooms. They are: ");
    List roomObjects = msg.getRoot().getChildren("room");
    
    List<RoomInfo> roomInfo = (List<RoomInfo>) state.getValue(ClientStateNames.K_ROOM_LIST);
    roomInfo.clear();
    for (Object obj : roomObjects) {
      Element elm = (Element) obj;
      String roomID = elm.getAttributeValue("id");
      String name = elm.getAttributeValue("name");
      int population = Integer.parseInt(elm.getAttributeValue("population"));      
      roomInfo.add(new RoomInfo(name, roomID, population));
    }
    bug("Whacking room list.");
    state.whackValue(ClientStateNames.K_ROOM_LIST);
  }

}
